import requests
from bs4 import BeautifulSoup


# 做了反爬爬取不到了，pandas好像可以？？？
url= "http://www.xinfadi.com.cn/index.html"
headers={
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
}
resp = requests.get(url, headers=headers)
resp.encoding = "utf-8"

soup = BeautifulSoup(resp.text, 'html.parser')

price_table = soup.find('tbody', {'class': 'ul'})
rows = price_table.find_all('tr')

for row in rows:
    columns = row.find_all('td')
    if len(columns) >= 4:  # 确保至少有菜品名和三个价格信息
        prod_name = columns[0].get_text()
        low_price = columns[1].get_text()
        avg_price = columns[3].get_text()
        high_price = columns[2].get_text()
        print(f"菜品：{prod_name}，最低价：{low_price}，平均价：{avg_price}，最高价：{high_price}")


